package com.itheima.health.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.dao.PermissionDao;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.pojo.Permission;
import com.itheima.health.service.PermissionService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import org.springframework.transaction.annotation.Transactional;

@DubboService
@Slf4j
public class PermissionServiceImpl implements PermissionService {
    @Autowired
    private PermissionDao permissionDao;
    @Override
    public List<Permission> findAll() {
        return permissionDao.findAll();
    }

    @Override
    public Integer[] findPermissionIdsByRoleId(Integer id) {
        return permissionDao.findPermissionIdsByRoleId(id);
    }



    @Transactional
    @Override
    public void add(Permission permission) {
        log.info("[权限-添加]data:{}",permissionDao);
        // 插入基本数据
        permissionDao.insert(permission);
    }

    @Transactional
    @Override
    public void delete(Integer id) {
        log.info("[权限-删除]data:{}",id);
        // 删除基本数据
        permissionDao.deleteById(id);
    }

    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        log.info("[权限-分页查询]data：{}",queryPageBean);
        //设置分页参数
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        //查询page
        Page<Permission> page= permissionDao.selectByCondition(queryPageBean.getQueryString());
        //封装返回值
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public Permission findById(Integer id) {
        return permissionDao.selectById(id);
    }

    @Transactional
    @Override
    public void edit(Permission permission) {
        //1、更新权限
        permissionDao.update(permission);
    }

}
